﻿using System;
using System.Collections.Generic;
using RentalSystem.Data.EF.Core;

namespace RentalSystem.Data.EF.Dao
{
    public class EFStatisticsDao : IStatisticsDao
    {
        #region Queries

        private readonly string ListQuery;
        private readonly string GetByIdQuery;
        private readonly string GetByNameQuery;

        #endregion

        private EFManager<Statistics> manager;

        public EFStatisticsDao(RentalDatabaseEntities repository)
        {
            ListQuery = "SELECT VALUE s FROM Statistics AS s;";
            GetByIdQuery = "SELECT VALUE s FROM Statistics AS s WHERE s.StatisticID = @Id;";
            GetByNameQuery = "SELECT VALUE s FROM Statistics AS s WHERE s.StatisticName = @Name;";

            this.manager = new EFManager<Statistics>(repository);
        }

        #region IDao1Key

        public Statistics Add(Statistics entity)
        {
            manager.Database.AddToStatistics(entity);
            manager.Save();
            return entity;
        }

        public Statistics Get(int id)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Id", id)
                };

            var statsList = manager.SQLQueryToEntity(GetByIdQuery, parameters);

            foreach (Statistics stats in statsList)
            {
                return stats;
            }

            return null;
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public void Delete(Statistics entity)
        {
            throw new NotImplementedException();
        }

        public void Update(Statistics entity, uint fields)
        {
            throw new NotImplementedException();
        }

        public List<Statistics> List()
        {
            try
            {
                List<Statistics> list = new List<Statistics>();
                var stats = manager.SQLQueryToEntity(ListQuery);

                foreach (Statistics statistic in stats)
                {
                    list.Add(statistic);
                }

                return list;
            }
            catch (Exception exc)
            {
                throw new Exception("MySQLUsersDao - Get user by email: " + exc);
            }
        }

        #endregion

        public Statistics GetByName(string name)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Name", name)
                };

            var statsList = manager.SQLQueryToEntity(GetByNameQuery, parameters);

            foreach (Statistics stats in statsList)
            {
                return stats;
            }

            return null;
        }

        public void UpdateStats(string name, float valueToAdd)
        {
            Statistics stats = GetByName(name);

            if (stats != null)
            {
                stats.StatisticValue += (decimal)valueToAdd;
                manager.Save();
            }
        }
    }
}
